package com.example.administrator.newxlmedical;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import adapters.SetViewPagerAdapter;
import beans.NewUserInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import constants.Constants;
import fragments.CabinetNumberFragment;
import fragments.CompanyInfoFragment;
import fragments.CopyLogFragment;
import fragments.FactorySetFragment;
import fragments.HospitalInfoFragment;
import fragments.SelectHospitalFragment;
import fragments.VersionFragment;
import utils.ActivityUtil;
import utils.ObjectSaveUtil;
import utils.ViewUtil;

public class SetActivity extends BaseActivity {

    @BindView(R.id.setting_home)
    ImageView settingHome;
    @BindView(R.id.tl_take_and_return)
    TabLayout tabLayout;
    @BindView(R.id.vp_take_and_return)
    ViewPager viewPager;

    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private SetViewPagerAdapter adapter;
    private NewUserInfoBean.DataBean userInfoBean;
    private int roleInfoId = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        ButterKnife.bind(this);

    }

    @Override
    public void initView() {
        super.initView();
        userInfoBean = (NewUserInfoBean.DataBean) ObjectSaveUtil.readObject(this, Constants.USER_INFO_BEAN, Constants.USER_INFO_BEAN);
        if (userInfoBean != null) {
            roleInfoId = userInfoBean.getRoleInfoId();
        }

        HospitalInfoFragment infoFragment = new HospitalInfoFragment();
        FactorySetFragment setFragment = new FactorySetFragment();
        CabinetNumberFragment cabinetNumberFragment = new CabinetNumberFragment();
        CompanyInfoFragment companyInfoFragment = new CompanyInfoFragment();
        CopyLogFragment copyLogFragment = new CopyLogFragment();
        VersionFragment versionFragment = new VersionFragment();
        SelectHospitalFragment selectHospitalFragment = new SelectHospitalFragment();
        if (roleInfoId != 5) {
            fragmentList.add(infoFragment);
            fragmentList.add(companyInfoFragment);
            titles.add("医院信息");
            titles.add("关于星联");
        } else {
            fragmentList.add(infoFragment);
            fragmentList.add(setFragment);
            fragmentList.add(cabinetNumberFragment);
            fragmentList.add(copyLogFragment);
            fragmentList.add(versionFragment);
            fragmentList.add(selectHospitalFragment);
            fragmentList.add(companyInfoFragment);
            titles.add("医院信息");
            titles.add("厂商设置");
            titles.add("柜子编号");
            titles.add("拷贝日志");
            titles.add("版本更新");
            titles.add("物品查询");
            titles.add("关于星联");
        }

        adapter = new SetViewPagerAdapter(getSupportFragmentManager(), fragmentList, titles, this);

        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);
        if (roleInfoId != 5) {
            tabLayout.getTabAt(0).setCustomView(adapter.getTabView(0));
            tabLayout.getTabAt(1).setCustomView(adapter.getTabView(5));
        } else {
            tabLayout.getTabAt(0).setCustomView(adapter.getTabView(0));
            tabLayout.getTabAt(1).setCustomView(adapter.getTabView(1));
            tabLayout.getTabAt(2).setCustomView(adapter.getTabView(2));
            tabLayout.getTabAt(3).setCustomView(adapter.getTabView(3));
            tabLayout.getTabAt(4).setCustomView(adapter.getTabView(4));
            tabLayout.getTabAt(5).setCustomView(adapter.getTabView(5));
            tabLayout.getTabAt(6).setCustomView(adapter.getTabView(6));
        }

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                ViewUtil.setIndicator(tabLayout, 15, 15);
//                ViewUtil.reflex(tabLayout);
            }
        });

    }


    @Override
    protected void setListener() {
        super.setListener();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                ImageView imageView = (ImageView) customView.findViewById(R.id.iv_tablayout_custom);
                TextView textView = (TextView) customView.findViewById(R.id.tv_tablayout_custom);
                int position = tab.getPosition();
                if (roleInfoId != 5) {
                    if (position == 0) {
                        imageView.setImageResource(R.mipmap.yyxxin);
                    } else if (position == 1) {
                        imageView.setImageResource(R.mipmap.gyxlin);
                    }
                } else {
                    if (position == 0) {
                        imageView.setImageResource(R.mipmap.yyxxin);
                    } else if (position == 1) {
                        imageView.setImageResource(R.mipmap.csszin);
                    } else if (position == 2) {
                        imageView.setImageResource(R.mipmap.gzbhin);
                    } else if (position == 3) {
                        imageView.setImageResource(R.mipmap.kbrizhib);
                    } else if (position == 4){
                        imageView.setImageResource(R.mipmap.bbgxu);
                    }else if (position == 5){
                        imageView.setImageResource(R.mipmap.wpcxw);
                    }else if (position == 6){
                        imageView.setImageResource(R.mipmap.gyxlin);
                    }
                }
                textView.setTextColor(getResources().getColor(R.color.date_bg_color));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                ImageView imageView = (ImageView) customView.findViewById(R.id.iv_tablayout_custom);
                TextView textView = (TextView) customView.findViewById(R.id.tv_tablayout_custom);
                int position = tab.getPosition();
                if (roleInfoId != 5) {
                    if (position == 0) {
                        imageView.setImageResource(R.mipmap.yyxx);
                    } else if (position == 1) {
                        imageView.setImageResource(R.mipmap.gyxlb);
                    }
                } else {
                    if (position == 0) {
                        imageView.setImageResource(R.mipmap.yyxx);
                    } else if (position == 1) {
                        imageView.setImageResource(R.mipmap.cssz);
                    } else if (position == 2) {
                        imageView.setImageResource(R.mipmap.gzbh);
                    } else if (position == 3){
                        imageView.setImageResource(R.mipmap.kbrizhiw);
                    }else if (position == 4){
                        imageView.setImageResource(R.mipmap.bbgxw);
                    }else if (position == 5){
                        imageView.setImageResource(R.mipmap.wpcxb);
                    }else if (position == 6){
                        imageView.setImageResource(R.mipmap.gyxlb);
                    }
                }
                textView.setTextColor(getResources().getColor(R.color.tablayout_text_unselect));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @OnClick(R.id.setting_home)
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.setting_home) {
            ActivityUtil.jumpToActivity(SetActivity.this, MainActivity.class);
            finish();
        }
    }
}
